/*
 * calculation_server_test.cpp
 *
 *  Created on: 21/06/2011
 *      Author: Yotam and Gilaad
 */

#include "../solveEquationSystem.h"
#include "mtmtest.h"
#include "../matrix.h"
#include <stdarg.h>

Matrix initMatrix( int rows, int columns, ... ) {
	Matrix result( rows, columns );

	va_list vl;
	va_start( vl, columns );

	for( int r = 1; r <= rows; ++r ) {
		for( int c = 1; c <= columns; ++c ) {
			double cell = va_arg(vl,double);
			result(r, c) = cell;
		}
	}
	va_end( vl );
	return result;
}


bool testSolve(){
	Matrix A = initMatrix(2,2,
			1.0, 2.0,
			3.0, 4.0);
	Matrix B = initMatrix(2,1,
			1.0,
			1.0);
	Matrix C = initMatrix(2,2,
			4.0, 5.0,
			2.0, 2.0);
	Matrix D = initMatrix(2,1,
			2.0,
			2.0);
	Matrix E = initMatrix(4,4,
			1.0, 1.0, 1.0, 1.0,
			1.0, 1.0, 1.0, 1.0,
			2.0, 2.0, 2.0, 2.0,
			6.0, 6.0, 6.0, 6.0);

	SolveEquationSystem job1(1, 40, A, B);
	SolveEquationSystem job2(2, 65, C, D);
	Matrix CD = job2.execute();
	Matrix AB = job1.execute();
	Matrix resultAB = initMatrix(2,1,-1.0, 1.0);
	Matrix resultCD = initMatrix(2,1, 3.0,-2.0);
	//should be equal
	ASSERT_EQUALS(AB, resultAB);
	ASSERT_EQUALS(CD, resultCD);
	//should fail
	job1 = SolveEquationSystem(3, 10, E, C);
	ASSERT_THROW(Job::JobFailed,job1.execute());
	job2 = SolveEquationSystem(4, 47, A, E);
	ASSERT_THROW(Job::JobFailed,job2.execute());
	SolveEquationSystem job3(5, 98, E, B);
	ASSERT_THROW(Job::JobFailed,job3.execute());
	//fail again
	Matrix F = initMatrix(2,3,1.0,2.0,3.0,4.0,5.0,6.0);
	Matrix G = initMatrix(3,2,2.0,3.0,4.0,5.0,6.0,7.0);
	job1 = SolveEquationSystem(3, 10, F, B);
	ASSERT_THROW(Job::JobFailed,job1.execute());
	job2 = SolveEquationSystem(4, 47, G, B);
	ASSERT_THROW(Job::JobFailed,job2.execute());

	return true;
}

int main(){
	RUN_TEST(testSolve);
	return 0;
}
